
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author Circle
 */
public class ManagerStockDetail implements IManager<StockDetail> {

    public final String FileName = "StockDetail.txt";
    public ArrayList<StockDetail> lstObject;
    public int length;

    public ManagerStockDetail() {
        lstObject = new ArrayList<>();
        length = 0;
        File f = new File(FileName);
        try {
            if (!f.exists()) {
                f.createNewFile();
            } else {
                ObjectInputStream OIStream = new ObjectInputStream(new FileInputStream(FileName));
                while (true) {
                    StockDetail temp = (StockDetail) OIStream.readObject();
                    lstObject.add(temp);    
                    length++;                
                }
            }
        } catch (IOException | ClassNotFoundException ex) {
            System.out.printf("Can't open data StockDetail");
        }
    }

    @Override
    public StockDetail GetById(int id) {
        for (int i = 0; i < length; i++) {
            if (lstObject.get(i).getId() == id) {
                return lstObject.get(i);
            }
        }
        return null;
    }

    @Override
    public ArrayList<StockDetail> GetList() {
        return lstObject;
    }

    @Override
    public void AddToList(StockDetail obj) {
        lstObject.add(obj);
        length++;
    }

    @Override
    public boolean DeleteById(int id) {
        StockDetail temp = GetById(id);
        if (temp.getId() != -1) {
            lstObject.remove(id);
            length--;
            System.out.println("Delete success");
            return true;
        } else {
            System.out.println("Can't find id in database");
            return false;
        }
    }

    @Override
    public boolean ChangeInfo(int index, StockDetail obj) {
        try {
            lstObject.add(index, obj);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public void ShowInfoOfList() {
        if (length < 0) {
            System.out.println("Blank Data of Advisory Client");
        } else {
            for (int i = 0; i < length; i++) {
                System.out.print(i + 1);
                lstObject.get(i).toString();
            }
        }
    }

    @Override
    public boolean SaveDataToFile() {
        try {
            try (ObjectOutputStream OOStream = new ObjectOutputStream(new FileOutputStream(FileName))) {
                for (int i = 0; i < length; i++) {
                    OOStream.writeObject(lstObject.get(i));
                }
                OOStream.flush();
                OOStream.close();
            }
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}
